package com.train.course.service.impl;

import com.train.course.domain.VideoCollect;
import com.train.course.mapper.VideoCollectMapper;
import com.train.course.mapper.VideoEpisodeMapper;
import com.train.course.mapper.VideoCourseMapper;
import com.train.course.service.IVideoCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 视频收藏 服务层实现
 */
@Service
public class VideoCollectServiceImpl implements IVideoCollectService {

    @Resource
    private VideoCollectMapper videoCollectMapper;

    @Autowired
    private VideoEpisodeMapper videoEpisodeMapper;
    
    @Autowired
    private VideoCourseMapper videoCourseMapper;

    /**
     * 收藏视频 - 切换模式（已收藏则取消收藏）
     */
    @Override
    @Transactional
    public boolean collectVideo(Long userId, Long videoId) {
        // 检查是否已经收藏
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, videoId, "1");
        if (existingCollect != null) {
            // 已经收藏过了，执行取消收藏
            return uncollectVideo(userId, videoId);
        }

        // 插入收藏记录
        VideoCollect videoCollect = new VideoCollect();
        videoCollect.setUserId(userId);
        videoCollect.setVideoId(videoId);
        videoCollect.setVideoType("1"); // 视频集类型
        videoCollect.setCreateBy(String.valueOf(userId));
        
        int collectResult = videoCollectMapper.insertVideoCollect(videoCollect);
        if (collectResult <= 0) {
            return false;
        }

        // 增加视频收藏数
        int countResult = videoEpisodeMapper.incrementCollectCount(videoId);
        return countResult > 0;
    }

    /**
     * 取消收藏
     */
    @Override
    @Transactional
    public boolean uncollectVideo(Long userId, Long videoId) {
        // 检查是否已经收藏
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, videoId, "1");
        if (existingCollect == null) {
            return false; // 没有收藏记录
        }

        // 删除收藏记录
        int collectResult = videoCollectMapper.deleteVideoCollectByUserAndVideo(userId, videoId, "1");
        if (collectResult <= 0) {
            return false;
        }

        // 减少视频收藏数
        int countResult = videoEpisodeMapper.decrementCollectCount(videoId);
        return countResult > 0;
    }

    /**
     * 检查用户是否已收藏
     */
    @Override
    public boolean isCollected(Long userId, Long videoId) {
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, videoId, "1");
        return existingCollect != null;
    }
    
    /**
     * 收藏课程 - 切换模式（已收藏则取消收藏）
     */
    @Override
    @Transactional
    public boolean collectCourse(Long userId, Long courseId) {
        // 检查是否已经收藏
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, courseId, "1");
        if (existingCollect != null) {
            // 已经收藏过了，执行取消收藏
            return uncollectCourse(userId, courseId);
        }

        // 插入收藏记录
        VideoCollect videoCollect = new VideoCollect();
        videoCollect.setUserId(userId);
        videoCollect.setVideoId(courseId);
        videoCollect.setVideoType("1"); // 课程类型
        videoCollect.setCreateBy(String.valueOf(userId));
        
        int collectResult = videoCollectMapper.insertVideoCollect(videoCollect);
        if (collectResult <= 0) {
            return false;
        }

        // 增加课程收藏数
        int countResult = videoCourseMapper.incrementCollectCount(courseId);
        return countResult > 0;
    }

    /**
     * 取消收藏课程
     */
    @Override
    @Transactional
    public boolean uncollectCourse(Long userId, Long courseId) {
        // 检查是否已经收藏
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, courseId, "1");
        if (existingCollect == null) {
            return false; // 没有收藏记录
        }

        // 删除收藏记录
        int collectResult = videoCollectMapper.deleteVideoCollectByUserAndVideo(userId, courseId, "1");
        if (collectResult <= 0) {
            return false;
        }

        // 减少课程收藏数
        int countResult = videoCourseMapper.decrementCollectCount(courseId);
        return countResult > 0;
    }

    /**
     * 检查用户是否已收藏课程
     */
    @Override
    public boolean isCourseCollected(Long userId, Long courseId) {
        VideoCollect existingCollect = videoCollectMapper.selectVideoCollectByUserAndVideo(userId, courseId, "1");
        return existingCollect != null;
    }
}